package com.gopay.checkorder.service.impl.expay;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.expay.CmbcExPayMessage;
import com.gopay.common.domain.bank.cmbc.expay.samebank.CmbcAdPayMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.TransUtil;
import com.gopay.remote.bank.ExPayInteractRemoteService;

/**
 * 民生代扣订单查询
 * 
 * @CheckOrderCmbcAdPayImpl.java
 * @author zhengxue
 * @2015-11-12 下午4:15:35 www.gopay.com.cn Inc.All rights reserved.
 */
public class CheckOrderCmbcAdPayImpl implements BankOrderService {
    private final static Logger logger = LoggerFactory.getLogger("CMBC");
    @Resource(name = "bankExPayClient")
    private ExPayInteractRemoteService bankExPayClient;

    @Override
    public CheckOrderResult checkOrder(BankOrder bankOrder, String arg1) {
        CheckOrderResult checkOrderResult = new CheckOrderResult();
        checkOrderResult.setSuccess(false);
        checkOrderResult.setOrderId(bankOrder.getOrderId());

        CmbcAdPayMessage message = new CmbcAdPayMessage();
        String merDate = bankOrder.getOrderId().substring(0, 8);
        message.setOriOrderDate(merDate);
        message.setOrderId(bankOrder.getOrderId()); // 国付宝系统订单号
        message.setBankOrderId(bankOrder.getOutOrderId());
        message.setBank(bankOrder.getBankCode());
        message.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
        message.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));
        message.setOrderDate(DateUtils.format(bankOrder.getSendBankTm(), "yyyyMMddHHmmss"));

//        Result result = new Result();
//        try {
//            result = bankExPayClient.queryOrder(message);
        // 民生代扣对账机制改为异步处理 add by zhengxue
        bankExPayClient.queryOrder(message);
        checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5112);
        checkOrderResult.setErrMsg("等待银行异步通知，请刷新页面重新点击查询按钮查看结果");
        return checkOrderResult;
//            if (result == null || !result.isSuccess()) {
//                logger.error("bankExPayClient fail.");
//                if (result == null || ProcCodeConstants.PROC_CODE_100E6058.equals(result.getResultCode())) {
//                    BankCountDownLatch.failIncrease(bankOrder.getBankCode());
//                }
//                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
//                checkOrderResult.setErrMsg("系统返回对账结果为空");
//                return checkOrderResult;
//            }
//
//            CmbcExPayMessage res = (CmbcExPayMessage) result.getDefaultModel();
//            if (res == null) {
//                // 查询结果为空
//                logger.error("fail:orderId:" + bankOrder.getOrderId() + " errcode:"
//                        + ProcCodeConstants.PROC_CODE_100E6022 + " err:bis query is null.");
//                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
//                checkOrderResult.setErrMsg("银行返回对账结果为空");
//                return checkOrderResult;
//            }
//
//            if (res.isFlag()) {
//                if (CmbcConstants.RESP_TYPE_SUCCESS.equals(res.getRetType())
//                        && CmbcConstants.EXEC_CODE_SUCCESS.equals(res.getRetCode())
//                        && CmbcConstants.RESP_TYPE_SUCCESS.equals(res.getOriRetType())
//                        && CmbcConstants.EXEC_CODE_SUCCESS.equals(res.getOriRetCode())) {
//                    // 交易成功
//                    checkOrderResult.setSuccess(true);
//                    InAccInfo accInfo = new InAccInfo();
//                    accInfo.setOrderId(bankOrder.getOrderId());
//                    String amt = TransUtil.amountConvert2Yuan(res.getOriAmt());
//                    accInfo.setAmount(new BigDecimal(amt));
//                    accInfo.setRetCode("00");
//                    accInfo.setCardType(BankCardType.Unknown.toString());
//                    checkOrderResult.setAccInfo(accInfo);
//                    logger.error("民生代扣支付订单：" + bankOrder.getOrderId() + "的交易成功" + "返回码：" + res.getOriRetCode()
//                            + "返回信息：" + res.getOriRetMsg());
//                } else if (CmbcConstants.RESP_TYPE_SUCCESS.equals(res.getRetType())
//                        && CmbcConstants.EXEC_CODE_SUCCESS.equals(res.getRetCode())
//                        && CmbcConstants.RESP_TYPE_ERROR.equals(res.getOriRetType())
//                        && CmbcConstants.EXEC_CODE_NOTEXIST.equals(res.getOriRetCode())) {
////                    checkOrderResult.setBankFailure(true);
//                    checkOrderResult.setErrMsg("银行返回交易失败");
//                    logger.error("民生代扣支付订单：" + bankOrder.getOrderId() + "的交易失败" + "返回码：" + res.getOriRetCode()
//                            + "返回信息：" + res.getOriRetMsg());
//                } else {
//                    checkOrderResult.setErrMsg("银行返回交易处理中");
//                    logger.error("民生代扣支付订单：" + bankOrder.getOrderId() + "的交易未知，系统暂不处理" + "返回码：" + res.getOriRetCode()
//                            + "返回信息：" + res.getOriRetMsg());
//                }
//            } else {
//                checkOrderResult.setErrMsg("银行返回验签失败");
//                logger.error("民生代扣支付订单：" + bankOrder.getOrderId() + "验签失败" + "返回码：" + res.isFlag());
//            }
//            return checkOrderResult;
//        } catch (Exception e) {
//            logger.error(bankOrder.getOrderId() + ": bankExPayClient fail.", e);
//            checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
//            checkOrderResult.setErrMsg("民生代扣对账异常");
//            return checkOrderResult;
//        }
    }

    @Override
    public BankOrder getDbOrder() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BaseMessage dayend(String bank_bus) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result getDayendMsg(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<CheckOrderResult> checkDayend(String bank_bus) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
        // TODO Auto-generated method stub
        return null;
    }

}
